* Run analyses using aggregate data
	
use "Empirics/AggregateData/Database/output/Database.dta", clear
cd "Empirics/AggregateData/Analysis"
set more off
graph drop _all

*. Run the following analyses:
	
local runregressions     =1    // Tables A.4, A.6, A.8
local pretrends          =1    // Table B.4 (requires runregressions=1)
local graphs_average     =1    // Figures 1, 2, 5 (left panel), A.7 (left panel), B.4, B.5
local GraphsByEpi        =1    // Figures B.1 and B.2 (requires graphs_average=1)
local PVSQ               =1    // Figures 3, 5 (right panel), A.2, and Table 2 (requires graphs_average=1)
local eventstudy         =1    // Figure A.6,Table A.5
local LP                 =1    // Figures A.8 and A.9
local fin2008            =1    // Figure A.7 (right panel)
local Mexico_RER_Growth  =1    // growth in RER between avg of 1995-99 and 1994
local Mexico_ImportShares=1    // Tables 5 and A.7 (some statistics)
local Mexico_NTShare     =1    // Tables 5 (statistic)
local Argentina_ImportShares=1 // Table A.7 (some statistics)

*. Where to store results
local outputfolder "../../../Output"
capture mkdir `outputfolder'

*. Set Window length
local windowstart=-4
local windowend=8

*. Minimum # years events need to be apart
local apart=4 // baseline

*. Create variables & Declare panel
qui do "Analysis_dofiles/CreateVars.do" `outputfolder'

*. Identify Large Devaluations
gen largeDevaluation = 1 if gER<=-0.20 & acceER<=-0.10 & gRER<=0

* Ensure events are X years apart, where X is given by `apart'
qui do "Analysis_dofiles/Apart.do" `apart' `outputfolder' `windowstart' `windowend' 

* Additional sample adjustments
qui do "Analysis_dofiles/Adjustments.do"

*. Extract list of events and number of events for each country
qui do "Analysis_dofiles/ExtractList.do"

*. Generate Table 1:
qui do "Analysis_dofiles/Table1.do" `outputfolder'

sort id year

*. Create more variables:
qui do "Analysis_dofiles/CreateMoreVars.do"

if `runregressions'==1{
	do "Analysis_dofiles/Regressions.do" `outputfolder' `pretrends'
}

if `eventstudy'==1{
	qui do "Analysis_dofiles/EventStudy.do" `outputfolder' `windowstart' `windowend'
}

if `LP'==1{
qui do "Analysis_dofiles/Local projections.do" `outputfolder'
}

if `fin2008'==1{
qui do "Analysis_dofiles/FinancialCrisis2008" `outputfolder' `GraphsByEpi' `windowstart' `windowend'
}

if `graphs_average'==1{
	qui do "Analysis_dofiles/Figures.do" `outputfolder' `GraphsByEpi' `windowstart' `windowend'
if `PVSQ'==1{
	qui do "Analysis_dofiles/PVSQ.do" `outputfolder' `GraphsByEpi' `windowstart' `windowend'
	}
}

if `Mexico_RER_Growth'==1{
qui do "Analysis_dofiles/Mexico_RER_Growth.do" `outputfolder'
}

if `Mexico_ImportShares'==1{
qui do "Analysis_dofiles/Mexico_ImportShares.do" `outputfolder'
}

if `Mexico_NTShare'==1{
qui do "Analysis_dofiles/NT_Classification.do"
qui do "Analysis_dofiles/Mexico_NT_share.do" `outputfolder'
}

if `Argentina_ImportShares'==1{
qui do "Analysis_dofiles/Argentina_ImportShares.do" `outputfolder'
}

cd ../../..
